/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import Beans.BillItemINPariod;
import Beans.InventoryStoreBean;
import Beans.ItemListBean;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import newkfp.AbstractFacade;
import newkfp.Bill;
import newkfp.BillItem;
import newkfp.Item;

/**
 *
 * @author A7med Kotb
 */
@Stateless
public class Inventory_StoreFacade extends AbstractFacade<InventoryStoreBean> {

    @PersistenceContext(unitName = "KFPNEWPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public Inventory_StoreFacade() {
        super(InventoryStoreBean.class);
    }

    public List<String> getItemTypes() {
        Query query = em.createNamedQuery("Item.findItemType");
        return query.getResultList();
    }

    public HashMap getAllItems() {
        Query query = em.createNamedQuery("Item.findAll");
        HashMap hashMap = new HashMap();
        List<Item> items = query.getResultList();
        for (int i = 0; i < items.size(); i++) {
            Item item = items.get(i);
            hashMap.put(item.getIdItem(), new ItemListBean(item.getItemName(), item.getFactoryID().getFactoryName(), item.getQuantity(), item.getPk_Count(), 0, "Regular"));

        }
        return hashMap;
    }

    public HashMap getItemsFromBillInPariod(int i, Date dateNow, Date toDate) {
        HashMap hashMap = new HashMap();
        //0=> if customer
        //1=> for exporter
        //where date(createDate) between ? And ?
        String from = new SimpleDateFormat("yyyy/MM/dd").format(dateNow);

        String to = new SimpleDateFormat("yyyy/MM/dd").format(toDate);

        Query query = getEntityManager().createNativeQuery("select itemID,itemID,sum(itemAmount) itemAmount,sum(packageCount) packageCount,avg(itemPrice) itemPrice from bill_item where exportBill=? And billID IN (select idBill from bill where createDate between ? And ?) group by itemID,bill_item_status", BillItemINPariod.class);
        query.setParameter(1, i);
        query.setParameter(2, from);
        query.setParameter(3, to);
        System.out.println("select itemID,itemID,sum(itemAmount) itemAmount,sum(packageCount) packageCount,avg(itemPrice) itemPrice,bill_item_status from bill_item where exportBill=" + i + " And billID IN (select idBill from bill where createDate between " + from + " And " + to + ") group by itemID,bill_item_status");
        List<BillItemINPariod> list = query.getResultList();
        System.out.println("type=" + i + " size : " + list.size());
        for (int j = 0; j < list.size(); j++) {
            BillItemINPariod itemINPariod = list.get(j);
            if (itemINPariod.getBillItemStatus() == null || itemINPariod.getBillItemStatus().equalsIgnoreCase("Regular") || itemINPariod.getBillItemStatus().equalsIgnoreCase("") ) {
                hashMap.put(itemINPariod.getItemID() + "_1" , new ItemListBean("", "", itemINPariod.getItemAmount(), itemINPariod.getPackageCount(), itemINPariod.getItemPrice(), itemINPariod.getBillItemStatus()));

            } else if (itemINPariod.getBillItemStatus().equalsIgnoreCase("SalesReturn")) {
                hashMap.put(itemINPariod.getItemID() + "_2", new ItemListBean("", "", itemINPariod.getItemAmount(), itemINPariod.getPackageCount(), itemINPariod.getItemPrice(), itemINPariod.getBillItemStatus()));

            }


        }
        return hashMap;
    }

    public double getTotalCash(int i, Date dateNow, Date toDate) {
        double cash = 0;
        String exporter = " AND customerID=-1 ";
        String customer = " AND exporterID=-1 ";
        String general = "select sum(billTotal) from bill where createDate between ? And ? ";
//0=> if customer
        //1=> for exporter
        if (i == 0) {
            general += customer;
        } else {
            general += exporter;
        }
        //where date(createDate) between ? And ?
        String from = new SimpleDateFormat("yyyy/MM/dd").format(dateNow);
        String to = new SimpleDateFormat("yyyy/MM/dd").format(toDate);

        Query query = getEntityManager().createNativeQuery(general);
        query.setParameter(1, from);
        query.setParameter(2, to);
        System.out.println(general + " " + from + " " + to);
        List l = query.getResultList();
        if (l != null && l.size() > 0) {
            if (l.get(0) != null) {
                cash = new Double(l.get(0).toString());

            }
        } else {
            cash = 0;
        }
        return cash;
    }
}
